package com.su.song.myactiviy;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.su.song.myactiviy.Util.DataUtil;

public class SendActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_send;
    private Button btn_transmit;
    private TextView tv_response;
    private TextView tv_show;
    private static final String msend = "在不？，摸摸我的栗子球";
    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        tv_send = findViewById(R.id.tv_send);
        btn_transmit = findViewById(R.id.btn_trantsmit);
        btn_transmit.setOnClickListener(this);

        tv_response = findViewById(R.id.tv_response);//要发送的应答消息
        tv_response.setText(msend);
        Button btn_response = findViewById(R.id.btn_response);//应答按钮
        btn_response.setOnClickListener(this);
        tv_show = findViewById(R.id.tv_show);//显示接收的回复信息

        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                      if(result != null){
                          Intent intent = result.getData();
                          if(intent != null && result.getResultCode() == RESULT_OK){
                              Bundle bundle3 = new Bundle();
//                              bundle3 = getIntent().getExtras();这是错误的写法，和前面不一样
                              bundle3 = intent.getExtras();//这里intent内置传送了过来
                              String reply_time = bundle3.getString("reply_time");
                              String reply_text = bundle3.getString("reply_text");
                              String desc = String.format("接收消息：\n接收信息时间：%s\n接收信息内容：%s\n",reply_time,reply_text);
                              tv_show.setText(desc);
                          }
                      }
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();//显性意图
        if(v.getId() == R.id.btn_trantsmit){
            intent.setClass(this,ReceiveActivity.class);//调转
            bundle.putString("Request_time", DataUtil.getNowTime());//往盒子里封装获取时间
            bundle.putString("Request_text",tv_send.getText().toString());//将被获取信息转换成字符
            intent.putExtras(bundle);
            startActivity(intent);
        }
        else if(v.getId() == R.id.btn_response){
            intent.setClass(this,ReceiveActivity.class);
            bundle.putString("Response_time",DataUtil.getNowTime());
            bundle.putString("Response_text",msend);
            intent.putExtras(bundle);
            register.launch(intent);


        }


    }
}